<template>
  <div>
    <!-- 
          弹框 右上角的 X 是默认自带 ，它默认就是给visible的值赋值为false
          el-dialog
          before-close	关闭前的回调，会暂停 Dialog 的关闭	function(done)，done 用于关闭 Dialog
          visible	是否显示 Dialog，支持 .sync 修饰符	boolean	—	false
          title	Dialog 的标题，也可通过具名 slot （见下表）传入	
          center	是否对头部和底部采用居中布局	boolean	—	false
       -->
    <el-dialog
      :title="isAdd ? '添加菜单' : '编辑菜单'"
      :visible.sync="isShow"
      :before-close="cancel"
      center
    >
      <el-form :model="menuForm" :rules="rules" ref="menuForm">
        <el-form-item
          label="菜单名称"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="menuForm.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth" prop="pid">
          <el-select
            @change="changePid"
            v-model="menuForm.pid"
            placeholder="请选择"
          >
            <el-option label="顶级菜单" :value="0"></el-option>
            <el-option
              v-for="item in menulist"
              :key="item.id"
              :label="item.title"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <!-- 
            单选：
            v-model的值（以及数据类型）要和 label一直才能选中
         -->
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio disabled v-model="menuForm.type" :label="1">目录</el-radio>
          <el-radio disabled v-model="menuForm.type" :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item
          v-if="menuForm.type == 1"
          label="菜单图标"
          :label-width="formLabelWidth"
        >
          <!-- <el-input v-model="menuForm.icon" autocomplete="off"></el-input> -->
          <el-select v-model="menuForm.icon" placeholder="请选择">
            <el-option
              v-for="item in iconlist"
              :key="item"
              :label="item"
              :value="item"
            >
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
          <!-- <el-input v-model="menuForm.url" autocomplete="off"></el-input> -->
          <el-select v-model="menuForm.url" placeholder="请选择">
            <el-option
              v-for="item in routesIndex"
              :key="item.path"
              :label="item.meta.title"
              :value="item.path"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="menuForm.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="confirm" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入添加接口
import { menuAdd, menuEdit, getMenuInfo } from "../../request/api";
import { mapActions, mapGetters } from "vuex";
import {routesIndex} from "../../router";
export default {
  data() {
    return {
      formLabelWidth: "120px",
      //表单对象
      menuForm: {
        pid: 0, //上级分类编号  注意：顶级菜单-0 number
        title: "", //菜单名称string
        icon: "", //图标string
        type: 1, //类型 1目录2菜单number
        url: "", //菜单地址string
        status: 1, //状态   1正常2禁用   number类型number
      },
      routesIndex: routesIndex,
      iconlist:['el-icon-s-tools','el-icon-setting','el-icon-star-off','el-icon-goods'],
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }],
      },
      // options: [
      //   {
      //     value: 1,
      //     label: "系统管理",
      //   },
      //   {
      //     value: 2,
      //     label: "商城管理",
      //   },
      // ],
    };
  },
  computed: {
    ...mapGetters({
      menulist: "menu/getMenu",
    }),
  },
  props: ["isShow", "isAdd"],
  methods: {
    ...mapActions({
      getMenuAction: "menu/getMenuAction",
    }),
    //封装一个切换事件用于修改菜单类型
    changePid() {
      // console.log(this.menuForm.pid,'9999999')
      //type: 1, //类型 1目录2菜单
      this.menuForm.type = this.menuForm.pid == 0 ? 1 : 2;
    },
    //封装一个重置方法
    reset() {
      this.menuForm = {
        pid: 0, //上级分类编号  注意：顶级菜单-0 number
        title: "", //菜单名称string
        icon: "", //图标string
        type: 1, //类型 1目录2菜单number
        url: "", //菜单地址string
        status: 1, //状态   1正常2禁用   number类型number
      };
      //resetFields	对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.menuForm.resetFields();
    },
    //封装一个关闭弹框事件
    cancel() {
      this.reset();
      //改变父组件的值
      this.$emit("cancel", false);
    },
    //封装一个获取一条数据事件
    lookup(id) {
      //调用接口
      // console.log(id,'来自于list给父组件menu')
      getMenuInfo({ id }).then((res) => {
        // console.log(res,'rrrrrrrrr')
        this.menuForm = res.list;
        this.menuForm.id = id;
      });
    },
    //封装一个表单提交事件
    confirm() {
      // console.log(this.menuForm);
      this.$refs.menuForm.validate((val) => {
        if (val) {
          if (this.isAdd) {
            //添加流程
            // console.log(this.menuForm, "表单对象");
            menuAdd(this.menuForm).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                //关闭弹框
                this.cancel();
                //调用列表接口
                this.getMenuAction();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            //编辑流程
            menuEdit(this.menuForm).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                //关闭弹框
                this.cancel();
                //调用列表接口
                this.getMenuAction();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style scoped></style>
